using System;
using GammaJul.ReSharper.GammaXaml.Highlightings.CSharp;
using JetBrains.Annotations;
using JetBrains.DocumentModel;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Daemon;
using JetBrains.ReSharper.Daemon.Impl;
using JetBrains.ReSharper.Psi.CSharp.Tree;

[assembly: RegisterConfigurableSeverity(
	DpFieldMustBeStaticReadOnlyHighlighting.SeverityId,
	null,
	HighlightingGroupIds.CodeSmell,
	DpFieldMustBeStaticReadOnlyHighlighting.Description,
	DpFieldMustBeStaticReadOnlyHighlighting.Description,
	Severity.WARNING,
	false)]

namespace GammaJul.ReSharper.GammaXaml.Highlightings.CSharp {

	/// <summary>
	/// An highlighting that represent a DependencyProperty field registration that isn't static or read-only.
	/// </summary>
	[ConfigurableSeverityHighlighting(SeverityId, CSharpProjectFileType.Name, OverlapResolve = OverlapResolveKind.WARNING)]
	public class DpFieldMustBeStaticReadOnlyHighlighting : IHighlightingWithRange {
		internal const string SeverityId = "Dp.FieldMustBeStatic";
		internal const string Description = "Dependency property field must be static read-only.";

		private readonly IFieldDeclaration _fieldDeclaration;

		/// <summary>
		/// Gets the dependency property field declaration.
		/// </summary>
		[NotNull]
		public IFieldDeclaration FieldDeclaration {
			get { return _fieldDeclaration; }
		}

		public string ToolTip {
			get { return Description; }
		}

		public string ErrorStripeToolTip {
			get { return Description; }
		}

		public int NavigationOffsetPatch {
			get { return 0; }
		}

		public bool IsValid() {
			return _fieldDeclaration.IsValid()
				&& (!_fieldDeclaration.IsStatic || !FieldDeclaration.IsReadonly);
		}

		public DocumentRange CalculateRange() {
			return _fieldDeclaration.NameIdentifier.GetHighlightingRange();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="DpFieldMustBeStaticReadOnlyHighlighting"/> class.
		/// </summary>
		/// <param name="fieldDeclaration">The dependency property field declaration.</param>
		public DpFieldMustBeStaticReadOnlyHighlighting([NotNull] IFieldDeclaration fieldDeclaration) {
			if (fieldDeclaration == null)
				throw new ArgumentNullException("fieldDeclaration");
			_fieldDeclaration = fieldDeclaration;
		}
	}

}
